<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-folder"></i> 资源管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-circle-plus-outline" class="handle-del mr10" @click="dialogFormVisible = true"
                    >新增资源</el-button
                >
                <el-select v-model="query.id" clearable @change="changeState" placeholder="分类" class="handle-select mr10">
                    <el-option v-for="item in optionList"  :key="item.id" :label="item.classifyName" :value="item.id"></el-option>
                </el-select>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                header-cell-class-name="table-header"
            >
                <el-table-column label="资源照片(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.pic" :preview-src-list="[scope.row.pic]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="resName" label="资源名称"></el-table-column>
                <el-table-column prop="resDesc" label="资源描述"></el-table-column>
                <el-table-column prop="resLink" label="资源链接"></el-table-column>
                <el-table-column prop="tag" label="资源标签"></el-table-column>
                <el-table-column prop="classify" label="资源分类"></el-table-column>
                <el-table-column prop="views" label="浏览量"></el-table-column>
                <el-table-column prop="comments" label="评论量"></el-table-column>
                <el-table-column prop="issueTime" label="发布时间"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.size"
                    :total="query.total"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <el-dialog title="添加资源" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-upload
                class="avatar-uploader"
                style="margin-left: 120px;width: 200px;"
                action="http://42.192.178.221:8088/api/user/avatar"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
                <el-form-item label="资源名称" :label-width="formLabelWidth">
                    <el-input clearable v-model="form.resName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源描述" :label-width="formLabelWidth">
                    <el-input clearable v-model="form.resDesc" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源链接" :label-width="formLabelWidth">
                    <el-input clearable v-model="form.resLink" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源照片" :label-width="formLabelWidth">
                    <el-input clearable v-model="form.pic" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源标签" :label-width="formLabelWidth">
                    <el-input clearable v-model="form.tag" autocomplete="off"></el-input>
                </el-form-item>
               
                <el-form-item label="资源分类" :label-width="formLabelWidth">
                    <el-select v-model="form.classify" clearable  class="handle-select mr10">
                        <el-option v-for="item in optionList"  :key="item.id" :label="item.classifyName" :value="item.classifyName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="insertRes">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { list,delRes,addRes,listClassify} from '../../api/resource.js';
export default {
    data() {
        return {
            query: {
                current: 1,
                size: 10,
                total: 0,
                id:1
            },
            tableData: [],
            params: {
                uid: '',
                token: ''
            },
            formLabelWidth: '120px',
            form: {
                resName:'',
                resDesc:'',
                resLink:'',
                pic:'',
                tag:'',
                classify:''
            },
            imageUrl: '',
            dialogFormVisible: false,
            optionList:[]
        };
    },
    created() {
        this.params.uid = this.$cookies.get('uid');
        this.params.token = this.$cookies.get('token');
        this.getPage();
        listClassify(this.params).then(res=>{
            if(res.code==200){
                this.optionList=res.data;
            }
        })
    },
    methods: {
        //添加资源
        insertRes(){
            if(this.form.pic.length>0){
                addRes(this.form,this.params).then(res=>{
                if (res.code==200) {
                    this.$notify({
                        title: '成功',
                        message: res.msg,
                        type: 'success'
                    });
                    this.form.pic='';
                    this.form.resName='';
                    this.form.resDesc='';
                    this.form.resLink='';
                    this.form.tag='';
                    this.form.classify='';
                    this.imageUrl='';
                    this.dialogFormVisible=false;
                    this.getPage();
                }else{
                    this.$notify({
                        title: '失败',
                        message: res.msg,
                        type: 'error'
                    }); 
                }
                })
            }
        },
         //根据状态查询
         changeState(){
            this.getPage();
        },
        //分页查询
        getPage() {
            list(this.query,this.params)
                .then((res) => {
                    if (res.code==200) {
                        this.query.current = res.data.current;
                        this.query.size = res.data.size;
                        this.query.total = res.data.total;
                        this.tableData = res.data.records;
                    }
                })
        },
        // 触发搜索按钮
        handleSearch() {
            this.getPage();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    delRes(row,this.params)
                        .then((res) => {
                            if (res.code==200) {
                                this.$notify({
                                    title: '成功',
                                    message: res.msg,
                                    type: 'success'
                                });
                                this.tableData.splice(index, 1);
                            }else{
                                this.$notify({
                                    title: '失败',
                                    message: res.msg,
                                    type: 'error'
                                }); 
                            }
                        })
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.query.current = val;
            this.getPage();
        },
        handleAvatarSuccess(res, file) {
            this.form.pic=res.data;
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
}
.header .logo {
    float: left;
    width: 200px;
    padding-left: 35px;
    line-height: 70px;
}
.header-right {
    float: right;
    padding-right: 50px;
}
.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #fff;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #fff;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
.content-title {
    font-weight: 400;
    line-height: 50px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
}
.pre-img {
    width: 80px;
    height: 80px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 40px;
}
.crop-demo {
    display: flex;
    align-items: flex-end;
}
.crop-demo-btn {
    position: relative;
    width: 80px;
    height: 30px;
    line-height: 30px;
    padding: 0 15px;
    margin-left: 20px;
    margin-bottom: 25px;
    background-color: #409eff;
    color: #fff;
    font-size: 12px;
    border-radius: 4px;
    box-sizing: border-box;
}
.crop-input {
    position: absolute;
    width: 100px;
    height: 40px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
